package com.pine.web.pineframework.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.stp.StpUtil;
import com.pine.common.beans.ResultVo;
import com.pine.common.beans.pineframework.request.SysAuthValidateCaptchaCodeRequest;
import com.pine.common.beans.pineframework.request.SysUserLoginRequest;
import com.pine.common.beans.pineframework.response.SysAuthGetCaptchaCodeResponse;
import com.pine.common.beans.pineframework.response.SysConfigQueryResponse;
import com.pine.common.beans.pineframework.response.SysUserLoginResponse;
import com.pine.common.systemlog.annotation.SysLog;
import com.pine.common.util.user.UserInfo;
import com.pine.common.util.user.UserUtil;
import com.pine.dao.pineframework.constants.StatusEnum;
import com.pine.manager.core.StringPool;
import com.pine.manager.pineframework.trigger.SysUserTriggerManager;
import com.pine.service.pineframework.SysAuthService;
import com.pine.service.pineframework.SysConfigService;
import com.pine.web.core.RateLimit;
import com.pine.web.core.WithoutLogin;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * <p>
 * 系统认证前端控制器
 * </p>
 *
 * @author pine manage
 * @since 2024-08-14
 */
@Tag(name = "系统认证前端控制器")
@RestController
@RequestMapping("/sys/auth")
public class SysAuthController {

    @Resource
    private SysAuthService sysAuthService;
    @Resource
    private SysConfigService sysConfigService;

    /**
     * 用户登录
     *
     * @param request 请求体
     * @return 响应体
     */
    @Operation(summary = "用户登录")
    @PostMapping("/login")
    @SysLog("用户登录")
    @WithoutLogin
    @SaIgnore
    public ResultVo<SysUserLoginResponse> login(@Valid @RequestBody SysUserLoginRequest request) {
        // 读取系统配置
        SysConfigQueryResponse sysConfig = sysConfigService.getOneByUniqueKey(StringPool.SYS_CONFIG_VALID_CAPTCHA_CODE_FOR_USER_SWITCH, "configKey");
        if (Objects.isNull(sysConfig) || Objects.equals(StatusEnum.ENABLE.getCode(), sysConfig.getStatus())) {
            // 先验证验证码
            SysAuthValidateCaptchaCodeRequest captchaCodeRequest = new SysAuthValidateCaptchaCodeRequest();
            captchaCodeRequest.setCaptchaCode(request.getCaptchaCode());
            captchaCodeRequest.setCaptchaKey(request.getCaptchaKey());
            sysAuthService.validateCaptchaCode(captchaCodeRequest);
        }

        // 登录
        return ResultVo.success(SysUserTriggerManager.login(request));
    }


    /**
     * 用户登出
     */
    @SaCheckLogin
    @Operation(summary = "用户登出")
    @PostMapping("/logout")
    @SysLog("用户登出")
    public ResultVo<Boolean> logout() {
        UserInfo userInfo = UserUtil.get();
        StpUtil.logoutByTokenValue(userInfo.token());
        return ResultVo.success();
    }


    /**
     * 获取验证码
     *
     * @return 验证码信息
     */
    @Operation(summary = "获取验证码")
    @PostMapping("/getCaptchaCode")
    @SysLog("获取验证码")
    @WithoutLogin
    @SaIgnore
    @RateLimit
    public ResultVo<SysAuthGetCaptchaCodeResponse> getCaptchaCode() {
        return ResultVo.success(sysAuthService.getCaptchaCode());
    }

    /**
     * 校验验证码
     *
     * @param request 请求体
     * @return 响应体
     */
    @Operation(summary = "校验验证码")
    @PostMapping("/validateCaptchaCode")
    @SysLog("校验验证码")
    @WithoutLogin
    @SaIgnore
    @RateLimit
    public ResultVo<Boolean> validateCaptchaCode(@Valid @RequestBody SysAuthValidateCaptchaCodeRequest request) {
        return ResultVo.success(sysAuthService.validateCaptchaCode(request));
    }
}
